import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { BasePage } from '../basePage';
import { UserService } from './../../providers/UserService';
import { StorageService } from './../../providers/StorageService';

/**
 * Generated class for the RegisterPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-register',
  templateUrl: 'register.html',
})
export class RegisterPage extends BasePage {

  /** 注册后台参数*/
  userVo = {
    phone: '',
    captcha: '',
    pwd: '',
    pwd_again: '',
  };

  screenHeight: any;
  marginTop: any;

  btnName = '获取验证码';
  /** 倒计时秒数 */
  nums = 10;
  /**后台验证码请求参数 */
  params = {
    phone_number: '',
    captcha_type: 0,
  };

  isAgreeAgreement: boolean;

  /** 验证码按钮是否可以点击 */
  btnIsDisable = true;

  constructor(injector: Injector, private userservice: UserService, public stc: StorageService) {
    super(injector);
  }


  ionViewDidLoad() {
    console.log('ionViewDidLoad RegisterPage');
    this.isAgreeAgreement = false;
    this.screenHeight = screen.height;
    let num = this.screenHeight;
    this.marginTop = parseInt(num) * 0.4 + 'px';
    console.log(this.screenHeight + '和' + this.marginTop);
  }

 
  // 发送验证码
  async getCaptchaAPI() {
    var re = /^1[3|4|5|6|7|8|9]\d{9}$/;
    if (!re.test(this.userVo.phone)) {
      this.showAlert('电话号码格式不正确');
      return;
    }
    this.params.phone_number = this.userVo.phone;
    this.params.captcha_type = 1;

    //.......
    let loader = this.loadingCtrl.create({
      content: "",
    });
    loader.present();
    //请求后台登陆
    let result = await this.userservice.doGetCaptchaAPI(this.params);
    console.log(result);
    loader.dismiss();
    if (result.code != 1) {
      this.showAlert(result.msg);
      return;
    } else {
      this.showAlert(result.msg);
      this.nums = 50;
      this.btnIsDisable = false;
      let timeId = setInterval(() => {
        this.nums--;
        this.btnName = this.nums + '秒后可重新获取';
        if (this.nums < 0) {
          clearInterval(timeId);
          this.btnIsDisable = true;
          this.btnName = '点击发送验证码';
          this.nums = 50;
        }
      }, 1000); //一秒执行一次
    }
  }


  /**注册 */
  async doRegister() {
    //var re = /^1[34578]\d{9}$/;
    var re = /^1[3|4|5|6|7|8|9]\d{9}$/;
    if (!re.test(this.userVo.phone)) {
      this.showAlert('电话号码格式不正确');
      return;
    }


    if (this.userVo.pwd.trim().length == 0) {
      this.showAlert('密码不能为空');
      return;
    }

    // if (this.userVo.pwd != this.userVo.pwd_again) {
    //   this.showAlert("两次密码输入不一致");
    //   return;
    // }

    //var pswre = /^\w{6,16}$/;
    var pswre = /^([a-zA-Z0-9]|[!.@#$%^&*]){6,16}$/
    if (!pswre.test(this.userVo.pwd.trim())) {
      this.showAlert('密码必须要在6~12位之间');
      return;
    }

    if(this.userVo.captcha.trim().length == 0) {
      //this.popErrMsg('security_code', '验证码不能为空');
      this.showAlert('验证码不能为空');
      return;
    }

    if (!this.isAgreeAgreement) {
      this.showAlert("请先同意协议");
      return;
    }

    let loader = this.loadingCtrl.create({
      content: "",
    });
    loader.present();
    //请求后台登陆

    // let paramsdo = {
    //   phone: this.userVo.phone,
    //   captcha: this.userVo.captcha,
    //   pwd:this.userVo.pwd,
    //   pwd: Md5.hashStr(this.userVo.pwd).toString(),
    //   pwd_again: Md5.hashStr(this.userVo.pwd_again).toString(),
    //   type: this.userVo.type,
    // };    
    // console.log(paramsdo);
    let paramsdo = {
      phone_number: this.userVo.phone,
      user_pwd: this.userVo.pwd,
      captcha: this.userVo.captcha
    }
    console.log(paramsdo);

    let result = await this.userservice.doRegister(paramsdo);
    console.log(result)
    loader.dismiss();
    if (result.code != 1) {
      this.showAlert(result.msg);
      return;
    }
    this.stc.write('phone', this.userVo.phone);
    this.navCtrl.pop();
  }


  /*是否同意美甲协议*/
  agreeAgreement() {
    console.log("点击了同意美甲");
    this.isAgreeAgreement ? (this.isAgreeAgreement = false) : (this.isAgreeAgreement = true);
    console.log(this.isAgreeAgreement);
  }
  /*跳转到美甲协议页面*/
  toMJxieyi() {
    console.log("跳转到美甲协议页面")
  }
  /*跳转到登录页面*/
  toLogin() {
    this.navCtrl.pop();
  }

  dismissCLI() {
    this.viewCtrl.dismiss();
  }
}

